package publish

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PublishUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPublishUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishUpdateLogic {
	return &PublishUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PublishUpdateLogic) PublishUpdate(req *types.UpdatePublishRequest) (resp *types.PublishResponse, err error) {
	_, err = l.svcCtx.Card.PublishUpdate(l.ctx, &cardclient.PublishUpdateRequest{
		Id:                 req.Id,
		PublishTime:        req.PublishTime,
		AdminId:            req.AdminId,
		CustomerId:         req.CustomerId,
		SkinId:             req.SkinId,
		Count:              req.Count,
		BeginNumber:        req.BeginNumber,
		EndNumber:          req.EndNumber,
		Status:             req.Status,
		Type:               req.Type,
		ServiceNumber:      req.ServiceNumber,
		ServiceOrderNumber: req.ServiceOrderNumber,
		ServiceMoney:       req.ServiceMoney,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("编辑卡片发行信息失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.PublishResponse{Code: response.ErrCode, Message: "编辑卡片发行失败"}, nil
	}

	return &types.PublishResponse{
		Code:    response.SuccessCode,
		Message: "编辑卡片发行成功",
	}, nil
}
